import { ElMessage } from "element-plus";
import { store, mars3d, Cesium, graphicLayer, map } from "@/utils/global";
import { calcVolume } from "@/utils/draw/saveEditorDrawData";
/**
 * 标绘数据渲染器
 */
const renderer = (data: any) => {
    data.forEach((v: any) => {
        v.positions = JSON.parse(v.positions);
        v.style = JSON.parse(v.style);
        setGraphicToMap(v);
    });
};
export default renderer;
let xyzId = "";
/**
 * 设置属性参数
 */
const setPropertyParameter = (data: any, graphic: any) => {
    // console.log(graphic);
    let style: any = graphic.options.style;
    let text: any = "";
    try {
        if (style.label.isLength) {
            text = graphic.distance.toFixed(2) + " m";
            graphic.setStyle({
                label: {
                    text: text,
                },
            });
        }
    } catch (error) {}
    try {
        if (style.label.isArea) {
            if (graphic.type == "plane") {
                text = (style.dimensions_y * style.dimensions_x).toFixed(2) + " ㎡";
            } else if (graphic.type == "polygon") {
                if (style.label.unit == "1") {
                    text = graphic.area.toFixed(2) + " ㎡";
                }
                if (style.label.unit == "2") {
                    text = (graphic.area / 666.67).toFixed(2) + " 亩";
                }
                if (style.label.unit == "3") {
                    text = (graphic.area / 10000).toFixed(2) + "公顷";
                }
            } else {
                text = graphic.area.toFixed(2) + " ㎡";
            }
            graphic.setStyle({
                label: {
                    text: text,
                },
            });
        }
    } catch (error) {}
    try {
        if (style.label.isVolume) {
            text = calcVolume(graphic);
            graphic.setStyle({
                label: {
                    text: text,
                },
            });
        }
    } catch (error) {}

    graphic.setOptions({
        folderId: data.pid ? data.pid : data.folderId,
        icon: data.icon,
        plottingName: data.plottingName,
        plottingType: data.plottingType, // 标绘类型
        isCustom: data.isCustom, // 是否自定义样式
        name: data.name,
        number: data.number,
        id: data.id,
    });
    graphic.show = data.isShow == "1" ? true : false;
    graphicLayer.addGraphic(graphic);
    // console.log(graphic);
};
const setGraphicToMap = (v: any) => {
    let graphic: any = undefined;
    switch (v.type) {
        case "wall":
            /**
             * 立面文字-围栏
             */
            graphic = new mars3d.graphic.WallEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "rectangle":
            /**
             * 贴地旋转文字 - "跑马文字" - 矩形
             */
            graphic = new mars3d.graphic.RectangleEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            // 更新旋转文字属性
            if (v.plottingType == "rotateText") {
                let rotation = Cesium.Math.toRadians(30);
                function getRotationValue() {
                    rotation += 0.005;
                    return rotation;
                }
                graphic.setStyle({
                    rotation: new Cesium.CallbackProperty(getRotationValue, false),
                    stRotation: new Cesium.CallbackProperty(getRotationValue, false),
                });
            }
            setPropertyParameter(v, graphic);
            break;

        case "label":
            /**
             * 普通文字
             */
            graphic = new mars3d.graphic.LabelEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "billboard":
            /**
             * 图标点
             */
            graphic = new mars3d.graphic.BillboardEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "polyline":
            /**
             * 线段
             */
            // let style = v.style;

            graphic = new mars3d.graphic.PolylineEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });

            // console.log(graphic);
            setPropertyParameter(v, graphic);
            break;

        case "curve":
            /**
             * 曲线
             */
            graphic = new mars3d.graphic.CurveEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "polylineVolume":
            /**
             * 管道
             */
            graphic = new mars3d.graphic.PolylineVolumeEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "corridor":
            /**
             * 走廊
             */
            graphic = new mars3d.graphic.CorridorEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "road":
            /**
             * 道路
             */
            graphic = new mars3d.graphic.Road({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "plane":
            /**
             * 平面
             */
            graphic = new mars3d.graphic.PlaneEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "circle":
            /**
             * 圆-圆柱
             */
            graphic = new mars3d.graphic.CircleEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "polygon":
            /**
             * 多边形
             */

            graphic = new mars3d.graphic.PolygonEntity({
                positions: v.positions,
                style: v.style,
                id: v.id,
            });

            setPropertyParameter(v, graphic);
            break;
        case "regular":
            /**
             * 正多边形
             */
            // console.log(v);
            // return;
            graphic = new mars3d.graphic.Regular({
                // position: v.positions, // 渲染问题
                position: v.positions.length > 1 && v.positions.length != 3 ? v.positions[1] : v.positions, // 渲染问题
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "sector":
            /**
             * 扇形
             */
            graphic = new mars3d.graphic.Sector({
                position: v.positions, // 渲染问题
                positions: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "ellipse":
            /**
             * 椭圆-椭圆柱
             */
            graphic = new mars3d.graphic.EllipseEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            // console.log(graphic);
            setPropertyParameter(v, graphic);
            break;
        case "box":
            /**
             * 盒子
             */
            graphic = new mars3d.graphic.BoxEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "cylinder":
            /**
             * 圆锥
             */
            graphic = new mars3d.graphic.CylinderEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;

        case "ellipsoid":
            /**
             * 雷达及半球-球及椭球
             */
            graphic = new mars3d.graphic.EllipsoidEntity({
                position: v.positions,
                style: v.style,
                id: v.id,
            });
            setPropertyParameter(v, graphic);
            break;
        case "xyz":
            /**
             * 金字塔瓦片
             */
            if (!xyzId) {
                xyzId = v.id;
            }
            const tileLayer = new mars3d.layer.XyzLayer({
                url: v.layerUrl,
                minimumLevel: 12,
                minimumTerrainLevel: 12,
                maximumLevel: 21,
                id: v.id,
                rectangle: { xmin: v.xmin, xmax: v.xmax, ymin: v.ymin, ymax: v.ymax },
                flyTo: v.id == xyzId ? true : false,
            });
            tileLayer.show = v.isShow == "1" ? true : false;
            map.addLayer(tileLayer);
            // if (v.id == xyzId) {
            //     map.flyTo(tileLayer);
            // }
            break;
        default:
            ElMessage.error(`${v.name}渲染错误`);
    }
};
